"""Base classes for indicators."""

import datetime as dt
import numpy as np
import misc.quoteperiod as qp
import system.navalasystem as na
from misc import parts
from misc import timedata
from misc import misc
from block import block


class Indicator(object):
    ## External setup
    # This must be set at initialization
    name = None
    # Quote period. Use this if the indicator may be set to work with different
    # periods but is not bound to a particular one. Note that for this to take
    # effect, _period has to be None
    period = None
    
    ## Internal setup
    # Set this when the indicator is bound to a fixed period.
    _period = None
    # timedata.TimeData class, defaults to timedata.Generic. Database accesses
    # this property
    _TDClass = timedata.Generic

    def __init__(self, name=None):
        self.name = name

    def GetPeriod(self):
        return self.period if self._period is None else self._period

    def FormatValue(self, value):
        """Formats a given input value. Default format is "%6d"."""
        return "%6d" % value


################################################################################
   
class StockIndicator(Indicator):
    """Calculates a single value based on a reference datetime and a stock."""
    
    def GetRefDt(self):
        return misc.EndOfDay(dt.datetime.today()) if self._refDt is None \
               else self._refDt
    
    def SetRefDt(self, x):
        """Just sets _refDt."""
        self._refDt = x

    # Reference datetime defaults to today at 23h59
    _refDt = None
    
    def DoTheTimeDataShit(self, name):
        Q = timedata.Generic()
        Q.SetSize(1)
        Q.y[0] = self.GetValue(name)
        Q.timestamp[0] = misc.dt2ts(self.GetRefDt())
        return Q 
    
    def GetValue(self, name):
        raise NotImplementedError("Not implemented")

################################################################################


    
#    ## Setup
#    # Field name from TimeData to work on. Some indicators may have this fixed.
#    # In this case, GetTimeData() will access fields within the input
#    # directly. In other cases, the indicator may use GetY(), which will be
#    # sensitive to fieldName. Defaults to "close"
#    fieldName = "close"
#    
#    def GetTimeData(self, td):
#        raise NotImplementedError("Not implemented")
#
#    def GetY(self, td):
#        return td.__getattribute__(self.fieldName)






